<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="通用名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['tongYongMingCheng', validatorRules.tongYongMingCheng]"
                placeholder="请输入通用名称"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="商品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['shangPinMingCheng', validatorRules.shangPinMingCheng]"
                placeholder="请输入商品名称"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="药品成分" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['yaoPinChengFen', validatorRules.yaoPinChengFen]"
                rows="4"
                placeholder="请输入药品成分"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用药禁忌" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['yongYaoJinJi', validatorRules.yongYaoJinJi]"
                rows="4"
                placeholder="请输入用药禁忌"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="功能主治" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['gongNengZhuZhi', validatorRules.gongNengZhuZhi]"
                rows="4"
                placeholder="请输入功能主治"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用法用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['yongFaYongLiang', validatorRules.yongFaYongLiang]"
                placeholder="请输入用法用量"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="药品性状" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['yaoPinXingZhuang', validatorRules.yaoPinXingZhuang]"
                rows="4"
                placeholder="请输入药品性状"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="包装规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['baoZhuangGuiGe', validatorRules.baoZhuangGuiGe]"
                placeholder="请输入包装规格"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="不良反应" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['buLiangFanYing', validatorRules.buLiangFanYing]"
                rows="4"
                placeholder="请输入不良反应"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="储存条件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['chuCunTiaoJian', validatorRules.chuCunTiaoJian]"
                rows="4"
                placeholder="请输入储存条件"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="有效期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['youXiaoQi', validatorRules.youXiaoQi]"
                placeholder="请输入有效期"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="注意事项" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['zhuYiShiXiang', validatorRules.zhuYiShiXiang]"
                rows="4"
                placeholder="请输入注意事项"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="孕妇提醒" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['yunFuTiXing', validatorRules.yunFuTiXing]"
                placeholder="请输入孕妇提醒"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="儿童提醒" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['erTongTiXing', validatorRules.erTongTiXing]"
                placeholder="请输入儿童提醒"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="老人提醒" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['laoRenTiXing', validatorRules.laoRenTiXing]"
                placeholder="请输入老人提醒"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="相互作用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['xiangHuZuoYong', validatorRules.xiangHuZuoYong]"
                rows="4"
                placeholder="请输入相互作用"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="批准文号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['piZhunWenHao', validatorRules.piZhunWenHao]"
                placeholder="请输入批准文号"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="生产厂商" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['shengChanChangShang', validatorRules.shengChanChangShang]"
                placeholder="请输入生产厂商"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'GkakYpYaopinModal',
  components: {},
  data() {
    return {
      form: this.$form.createForm(this),
      disableSubmit: false,
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        tongYongMingCheng: { rules: [{ required: true, message: '请输入通用名称!' }] },
        shangPinMingCheng: { rules: [{ required: true, message: '请输入商品名称!' }] },
        yaoPinChengFen: { rules: [{ required: true, message: '请输入药品成分!' }] },
        yongYaoJinJi: { rules: [] },
        gongNengZhuZhi: { rules: [{ required: true, message: '请输入功能主治!' }] },
        yongFaYongLiang: { rules: [{ required: true, message: '请输入用法用量!' }] },
        yaoPinXingZhuang: { rules: [] },
        baoZhuangGuiGe: { rules: [] },
        buLiangFanYing: { rules: [] },
        chuCunTiaoJian: { rules: [] },
        youXiaoQi: { rules: [{ required: true, message: '请输入有效期!' }] },
        zhuYiShiXiang: { rules: [] },
        yunFuTiXing: { rules: [] },
        erTongTiXing: { rules: [] },
        laoRenTiXing: { rules: [] },
        xiangHuZuoYong: { rules: [] },
        piZhunWenHao: { rules: [{ required: true, message: '请输入批准文号!' }] },
        shengChanChangShang: { rules: [{ required: true, message: '请输入生产厂商!' }] }
      },
      url: {
        add: '/gkakYpYaopin/add',
        edit: '/gkakYpYaopin/edit'
      }
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'tongYongMingCheng',
            'shangPinMingCheng',
            'yaoPinChengFen',
            'yongYaoJinJi',
            'gongNengZhuZhi',
            'yongFaYongLiang',
            'yaoPinXingZhuang',
            'baoZhuangGuiGe',
            'buLiangFanYing',
            'chuCunTiaoJian',
            'youXiaoQi',
            'zhuYiShiXiang',
            'yunFuTiXing',
            'erTongTiXing',
            'laoRenTiXing',
            'xiangHuZuoYong',
            'piZhunWenHao',
            'shengChanChangShang'
          )
        )
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'tongYongMingCheng',
          'shangPinMingCheng',
          'yaoPinChengFen',
          'yongYaoJinJi',
          'gongNengZhuZhi',
          'yongFaYongLiang',
          'yaoPinXingZhuang',
          'baoZhuangGuiGe',
          'buLiangFanYing',
          'chuCunTiaoJian',
          'youXiaoQi',
          'zhuYiShiXiang',
          'yunFuTiXing',
          'erTongTiXing',
          'laoRenTiXing',
          'xiangHuZuoYong',
          'piZhunWenHao',
          'shengChanChangShang'
        )
      )
    }
  }
}
</script>
